package com.mbc.web.common.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.mbc.common.entities.ApplicationItem;
import com.mbc.web.common.client.ui.ContentTag;
import com.mbc.web.common.client.ui.FrmCurrency;
import com.mbc.web.common.client.ui.FrmCurrencyRateRpt;
import com.mbc.web.common.client.ui.FrmCurrencyReport;
import com.mbc.web.common.client.ui.FrmCurrencyRpt;
import com.mbc.web.common.client.ui.FrmLogin;
import com.mbc.web.common.client.ui.FrmUserBranchChange;
import com.mbc.web.common.client.ui.MenubarTag;
import com.mbc.web.common.client.ui.SearchBranchType;
import com.mbc.web.common.client.ui.SearchCountry;
import com.mbc.web.common.client.ui.SearchCurrency;
import com.mbc.web.common.client.ui.SearchBranch;
import com.mbc.web.common.client.ui.SearchCurrencyRate;
import com.mbc.web.common.client.ui.SearchRole;
import com.mbc.web.common.client.ui.SearchUser;
import com.mbc.web.common.client.ui.SearchUserBranchChange;
import com.mbc.web.common.client.ui.SidebarTag;

public class ClientSingleton {

	public static List<ApplicationItem> appItemList;
	public static ApplicationItem currentAppItem;
	private static ContentTag contentTag;
	
	public static ClickHandler loginClickHandler;
	public static ClickHandler menuClickHandler;
	
	private static String [] myanmarNumbers;
	private static String [] myanmarNumberValues;
	private static String myanmarCurrencyName;
	private static String myanmarDecName;
	private static String loginId;
	
	private static Composite homePage;
	
	public static void setHomePage(Composite composite){
		homePage = composite;
	}
	
	public static String getLoginId() {
		return loginId;
	}

	public static void setLoginId(String loginId) {
		ClientSingleton.loginId = loginId;
	}

	public static String[] getMyanmarNumbers() {
		return myanmarNumbers;
	}

	public static void setMyanmarNumbers(String[] myanmarNumbers) {
		ClientSingleton.myanmarNumbers = myanmarNumbers;
	}

	public static String[] getMyanmarNumberValues() {
		return myanmarNumberValues;
	}

	public static void setMyanmarNumberValues(String[] myanmarNumberValues) {
		ClientSingleton.myanmarNumberValues = myanmarNumberValues;
	}

	public static String getMyanmarCurrencyName() {
		return myanmarCurrencyName;
	}

	public static void setMyanmarCurrencyName(String myanmarCurrencyName) {
		ClientSingleton.myanmarCurrencyName = myanmarCurrencyName;
	}

	public static String getMyanmarDecName() {
		return myanmarDecName;
	}

	public static void setMyanmarDecName(String myanmarDecName) {
		ClientSingleton.myanmarDecName = myanmarDecName;
	}
	
	public static void changeView(Composite view){
		contentTag.showView(view);
	}
	
	public static void goHomePage(){
		if(homePage != null)
			contentTag.showHome(homePage);
	}
	
	public static boolean changeView() {
		if(currentAppItem.getCode().equals("ms_search_user"))
		{
			contentTag.showView(new SearchUser());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_search_role"))
		{
			contentTag.showView(new SearchRole());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_currency")){
			contentTag.showView(new FrmCurrency());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_search_currency")){
			contentTag.showView(new SearchCurrency());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_search_office")){
			contentTag.showView(new SearchBranch());
			return  true;
		}
		else if(currentAppItem.getCode().equals("ms_search_cur_rate")){
			contentTag.showView(new SearchCurrencyRate());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_currency_rpt")){
			contentTag.showView(new FrmCurrencyReport());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_currencyRpt_rpt")){
			contentTag.showView(new FrmCurrencyRpt());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_CRate_rpt")){
			contentTag.showView(new FrmCurrencyRateRpt());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_btype_search")){
			contentTag.showView(new SearchBranchType());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_country_search")){
			contentTag.showView(new SearchCountry());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_branch_change")){
			contentTag.showView(new FrmUserBranchChange());
			return true;
		}
		else if(currentAppItem.getCode().equals("ms_bc_list")){
			contentTag.showView(new SearchUserBranchChange());
			return true;
		}
	
		return false;
	}
	
	public static void setCurrentAppItem(String code){
		for (ApplicationItem l_item : appItemList) {
			if(code.equals(l_item.getCode())){
				currentAppItem = l_item;
				break;
			}
		}
	}

	public static void showLogin() {
		clearMainContent();
		RootPanel l_body = RootPanel.get("rootBody");
		l_body.getElement().setAttribute("style", "padding-left:0px");
		l_body.add(new FrmLogin());
		ViewUtils.prepareTabindexNEnter();
	}
	
	private static void clearMainContent(){
		RootPanel.get("mainContent").clear();
		jsClearMainContnet();
	}
	
	private static native void jsClearMainContnet()/*-{
		$wnd.$("#mainContent .breadcrumb").remove();
	}-*/;

	public static void showMainTags() {
		RootPanel.get("rootBody").getElement().removeAttribute("style");
		RootPanel.get("rootBody").clear();
		RootPanel l_mainContentDiv = RootPanel.get("mainContent");
		l_mainContentDiv.clear();
		l_mainContentDiv.add(new SidebarTag());
		l_mainContentDiv.add(new MenubarTag());
		l_mainContentDiv.getElement().appendChild((getBreadcrumb()));
		contentTag = new ContentTag();
		l_mainContentDiv.add(contentTag);
		goHomePage();
	}

	private static Element getBreadcrumb() {
		Element l_ol = DOM.createElement("ol");
		l_ol.setClassName("breadcrumb");

		Element l_li = DOM.createElement("li");
		Element l_a = DOM.createAnchor();
		l_a.setAttribute("href", "#");
		l_a.setInnerText("MBC");
		l_li.appendChild(l_a);
		l_ol.appendChild(l_li);

		Element l_li2 = DOM.createElement("li");
		l_li2.setClassName("active");
		l_li2.setInnerText("HOME");
		l_ol.appendChild(l_li2);
		return l_ol;
	}
	
	public static String formatNumber(String p, int decimal){
		return formatNumber(Double.parseDouble(p), decimal);
	}
	
	public static String formatNumber(double p, int decimal){
		String l_ret = "";
		String l_format = "###0";
		if(decimal > 0)
			l_format = "###0." + zero(decimal);
		l_ret = NumberFormat.getFormat(l_format).format(p);
		return l_ret;
	}
	
	public static String zero(int value)
	{
		String l_zeros = "";
		for (int i = 0; i < value; i++) {
			l_zeros += "0";
		}
		return l_zeros;
	}
	
	public static String amountInWords(String amount, boolean isHome)
	{
		String l_value = amount;
		String l_dec = "";
		if(amount.contains("."))
		{
			amount = amount.replace('.', ' ');
			String[] l_arr = amount.split(" ");
			l_value = l_arr[0];
			l_dec = l_arr[1];
		}
		if(isHome)
			return amountInWordsK(l_value, l_dec);
		else
			return amountInWords(l_value, l_dec);
	}
	
	private static native String amountInWordsK(String value, String decimal)/*-{
		return  $wnd.getNumber2k(value, decimal);
	}-*/;
	
	private static native String amountInWords(String value, String decimal)/*-{
		return  $wnd.getNumber2d(value, decimal);
	}-*/;
	
	private static String getPrefixWords(String num){
		String l_num1 = "100";
		String l_num2 = num;
		for (int i = 1; i < myanmarNumberValues.length; i++) {
			l_num1 += "0";
			l_num2 += "0";
		}
		if((Long.parseLong(l_num2) % Long.parseLong(l_num1)) == 0)
			return myanmarNumberValues[0] + getWords(num);
		else
			return getWords(num) + myanmarNumberValues[0];
	}
	
	private static String getWords(String num)
	{
		String l_output = "";
		char[] l_no = num.toCharArray();
		int l_diff = myanmarNumberValues.length - l_no.length;
		for (int i = 0; i < l_no.length; i++) {
			if(!String.valueOf(l_no[i]).equals("0")){
				String l_remainValue = num.substring(i + 1);
				if(l_remainValue.equals("") || Integer.parseInt(l_remainValue) == 0){
					l_output += " " + myanmarNumbers[Integer.parseInt(String.valueOf(l_no[i]))] + " " + myanmarNumberValues[ i + l_diff].split(":")[0];
				} else {
					String l_value = myanmarNumberValues[ i + l_diff];
					if(l_value.contains(":"))
						l_value = l_value.split(":")[1];
					l_output += " " + myanmarNumbers[Integer.parseInt(String.valueOf(l_no[i]))] + " " + l_value;
				}
			}
		}
		return l_output;
	}
	
	public static String amountInWordsMM(String value)
	{
		if(myanmarNumberValues == null || myanmarNumbers == null)
			return "";
		String l_output = "";
		String l_num = value;
		String l_dec = "0";
		if(value.contains("."))
		{
			value = value.replace('.', ' ');
			l_num = value.split(" ")[0];
			l_dec = value.split(" ")[1];
		}
		if(Double.parseDouble(l_num) > 0)
		{
			if(l_num.length() > myanmarNumberValues.length)
			{
				if(l_num.length() > ((myanmarNumberValues.length * 2) - 1))
					return "The amount is too large to convert into words.";
				
				int l_mod = l_num.length() % myanmarNumberValues.length;
				
				if(l_mod > 0)
				{
					String l_preNum = l_num.substring(0, l_mod + 1);
					l_output = getPrefixWords(l_preNum);
				}
				String l_postNum = l_num.substring(l_mod + 1);
				l_output += " " + getWords(l_postNum);
				l_output += " " + myanmarCurrencyName;
			}
			else
				l_output = getWords(l_num) + " " + myanmarCurrencyName;
			
		}
		if(Double.parseDouble(l_dec) > 0)
		{
			l_output += " " + getWords(l_dec) + " " + myanmarDecName;
		}
		return l_output;
	}
	
	public static void viewReport(String title){
		//String l_url = GWT.getModuleBaseURL() + "reportviewer.jsp?title=" + title;
		/**-- MBC --**/
		String l_url = GWT.getHostPageBaseURL() + "reportviewer.jsp?title=" + title + "&id=" + Random.nextInt();
		Window.open(l_url, "_blank", "");
	}
	public static Date getCurrentDateTime(Date date) {
		String c_date = DateTimeFormat.getFormat("dd/MM/yyyy").format(date);
		String c_time = DateTimeFormat.getFormat("h:mm a").format(new Date());
		
		String c_datetime = c_date + " " + c_time;
		
		return DateTimeFormat.getFormat("dd/MM/yyyy h:mm a").parse(c_datetime);
	}
	
	public static Date getCurrentDateTime() {
		return DateTimeFormat.getFormat("dd/MM/yyyy h:mm a").parse(DateTimeFormat.getFormat("dd/MM/yyyy h:mm a").format(new Date()));
	}
	
	public static Date getCurrentDateTime(Date date, String time) {
		String c_date = DateTimeFormat.getFormat("dd/MM/yyyy").format(date);
		String c_datetime = c_date + " " + time;
		return DateTimeFormat.getFormat("dd/MM/yyyy h:mm a").parse(c_datetime);
	}
}
